package com.elai.web.controller.baseconfig;

import com.elai.common.core.domain.entity.SysUser;
import com.elai.common.utils.SecurityUtils;
import com.elai.common.utils.poi.ExcelUtil;
import com.elai.common.annotation.Log;
import com.elai.common.core.controller.BaseController;
import com.elai.common.core.domain.AjaxResult;
import com.elai.common.core.page.TableDataInfo;
import com.elai.common.enums.BusinessType;
import com.elai.baseconfig.domain.BaseUserAttributesDO;
import com.elai.baseconfig.service.BaseUserAttributesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户属性Controller
 *
 * @author elai
 * @date 2021-06-06
 */
@RestController
@RequestMapping("/config/userattribute")
public class BaseUserAttributesController extends BaseController
{
    @Autowired
    private BaseUserAttributesService baseUserAttributesService;

    /**
     * 查询用户属性列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BaseUserAttributesDO baseUserAttributes)
    {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(!currentUser.isAdmin()){
            baseUserAttributes.setLesseeId(currentUser.getLesseeId());
        }
        startPage();
        List<BaseUserAttributesDO> list = baseUserAttributesService.selectBaseUserAttributesList(baseUserAttributes);
        return getDataTable(list);
    }

    @GetMapping("/getUserAttributeList")
    public AjaxResult getUserAttributeList(BaseUserAttributesDO baseUserAttributes)
    {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(!currentUser.isAdmin()){
            baseUserAttributes.setLesseeId(currentUser.getLesseeId());
        }
        List<BaseUserAttributesDO> list = baseUserAttributesService.selectBaseUserAttributesList(baseUserAttributes);
        return AjaxResult.success(list);
    }

    /**
     * 导出用户属性列表
     */
    @PreAuthorize("@ss.hasPermi('config:userattribute:export')")
    @Log(title = "用户属性", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BaseUserAttributesDO baseUserAttributes)
    {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(!currentUser.isAdmin()){
            baseUserAttributes.setLesseeId(currentUser.getLesseeId());
        }
        List<BaseUserAttributesDO> list = baseUserAttributesService.selectBaseUserAttributesList(baseUserAttributes);
        ExcelUtil<BaseUserAttributesDO> util = new ExcelUtil<BaseUserAttributesDO>(BaseUserAttributesDO.class);
        return util.exportExcel(list, "userattribute");
    }

    /**
     * 获取用户属性详细信息
     */
    @PreAuthorize("@ss.hasPermi('config:userattribute:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(baseUserAttributesService.selectBaseUserAttributesById(id));
    }

    /**
     * 新增用户属性
     */
    @PreAuthorize("@ss.hasPermi('config:userattribute:add')")
    @Log(title = "用户属性", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseUserAttributesDO baseUserAttributes)
    {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        baseUserAttributes.setLesseeId(currentUser.getLesseeId());
        return toAjax(baseUserAttributesService.insertBaseUserAttributes(baseUserAttributes));
    }

    /**
     * 修改用户属性
     */
    @PreAuthorize("@ss.hasPermi('config:userattribute:edit')")
    @Log(title = "用户属性", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseUserAttributesDO baseUserAttributes)
    {
        return toAjax(baseUserAttributesService.updateBaseUserAttributes(baseUserAttributes));
    }

    /**
     * 删除用户属性
     */
    @PreAuthorize("@ss.hasPermi('config:userattribute:remove')")
    @Log(title = "用户属性", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(baseUserAttributesService.deleteBaseUserAttributesByIds(ids));
    }
}
